﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodorderpaymentServices : Repository<corecmsfoodorderpayment>, IcorecmsfoodorderpaymentServices
    {
        public bool InsertAsync(corecmsfoodorderpayment entity)
        {
            return base.Change<corecmsfoodorderpayment>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodorderpayment> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodorderpayment entity)
        {
            return base.Change<corecmsfoodorderpayment>().Update(a => new corecmsfoodorderpayment()
            {
                orderId = entity.orderId,
                paymentId = entity.paymentId,
                paymentCode = entity.paymentCode,
                payAmount = entity.payAmount,
                outcomeType = entity.outcomeType,
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodorderpayment> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodorderpayment>().Update(a => new corecmsfoodorderpayment()
                {
                    orderId = item.orderId,
                    paymentId = item.paymentId,
                    paymentCode = item.paymentCode,
                    payAmount = item.payAmount
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodorderpayment>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodorderpayment>().DeleteById(id);
            }
            return returnKey;
        }
        public List<corecmsfoodorderpayment> SelectByOrderId(List<long> orderIds)
        {
            List<corecmsfoodorderpayment> lst = base.AsQueryable().Where(p => orderIds.Contains(p.orderId)).ToList();
            return lst;
        }
        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodorderpayment> GetCaChe()
        {
            return base.GetList();
        }

        #endregion


        public List<corecmsfoodorderpayment> QueryAsync(long orderId)
        {
            List<corecmsfoodorderpayment> list = new List<corecmsfoodorderpayment>();
            var where = Expressionable.Create<corecmsfoodorderpayment>();
            if (orderId > 0)
            {
                where = where.And(a => a.orderId == orderId);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }

        /// <summary>
        /// 前台支付明细统计(传后台报表库数据)
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodorderpayment> QueryOrderpayments(long orderId, sysuser sysUser)
        {
            var list1 = base.AsQueryable()
               .LeftJoin<corecmsfoodordermaster>((a, b) => a.orderId == b.orderId)
               .LeftJoin<corecmspayments>((a, b, c) => a.paymentId == c.id)
               .Where((a, b, c) => a.orderId == orderId )
               .Select((a, b, c) => new corecmsfoodorderpayment
               {
                   id = a.id,
                   orderId = a.orderId,
                   paymentId = a.paymentId,
                   paymentCode = a.paymentCode,
                   payAmount = a.payAmount,
                   payName = c.name,
                   storeId = b.storeId,
                   corpId = b.corpId,
                   payNo = a.payNo,
                   mchNo = a.mchNo,
                   refMchNo = a.refMchNo,
                   refPayNo = a.refPayNo,
                   tenantBalanceTypeId = c.tenantBalanceTypeId,
               }).With(SqlWith.NoLock).ToList();
            return list1;
        }

        /// <summary>
        /// 前台支付明细统计(传后台报表库数据)
        /// </summary>
        /// <returns></returns>
        public corecmsfoodorderpayment QueryOrderPayment(long orderId, int cashPay, ref List<corecmsfoodorderpayment> paymentList)
        {
            paymentList = base.AsQueryable()
               .LeftJoin<corecmspayments>((a, b) => a.paymentId == b.id)
               .Where((a, b) => a.orderId == orderId)
               .Select((a, b) => new corecmsfoodorderpayment
               {
                   id = a.id,
                   orderId = a.orderId,
                   paymentId = a.paymentId,
                   paymentCode = a.paymentCode,
                   payAmount = a.payAmount,
                   payName = b.name,
                   getBackAmount = a.takeAmount - a.payAmount,
                   takeAmount = a.takeAmount,
               }).With(SqlWith.NoLock).ToList();

            var model = new corecmsfoodorderpayment();
            model.orderId = orderId;
            model.payAmount = 0;
            model.payName = "";
            model.getBackAmount = 0;
            foreach (var item in paymentList)
            {
                if (!string.IsNullOrEmpty(model.payName)) model.payName = model.payName + ";";
                model.payName = model.payName + string.Format("{0}:{1}", item.payName, item.payAmount);
                if (item.paymentId == cashPay) model.getBackAmount += item.takeAmount - item.payAmount;
            }
            return model;
        }


    }
}
